package com.booking.springrestful.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.booking.springrestful.domain.Counselors;
import com.booking.springrestful.domain.Useranalytics;
import com.booking.springrestful.domain.Users;
import com.booking.springrestful.domain.dto.AppointmentsDto;
import com.booking.springrestful.domain.vo.AnalyticsMapVo;
import com.booking.springrestful.domain.vo.AppointmentRecordVo;
import com.booking.springrestful.mapper.AppointmentsMapper;
import com.booking.springrestful.mapper.CounselorsMapper;
import com.booking.springrestful.mapper.UseranalyticsMapper;
import com.booking.springrestful.mapper.UsersMapper;
import com.booking.springrestful.service.UseranalyticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 13182
* @description 针对表【useranalytics(对于咨询师的分析表)】的数据库操作Service实现
* @createDate 2024-11-06 19:54:03
*/
@Service
@Slf4j
public class UseranalyticsServiceImpl extends ServiceImpl<UseranalyticsMapper, Useranalytics>
    implements UseranalyticsService {

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private CounselorsMapper counselorsMapper;

    @Resource
    private UseranalyticsMapper useranalyticsMapper;

    @Override
    public List<AnalyticsMapVo> map() {

        ArrayList<AnalyticsMapVo> list = new ArrayList<>();
        List<Users> usersList = usersMapper.selectList(new QueryWrapper<Users>().eq("userType","client").or().eq("userType","counselor"));
        usersList.forEach(item->{
            AnalyticsMapVo vo = new AnalyticsMapVo();
            vo.setAddress(item.getAddress());
            vo.setUserType(item.getUsertype());
            list.add(vo);
        });
        return list;
    }



    @Override
    public void addTotal(AppointmentsDto appointments) {
        Integer userid = counselorsMapper.selectOne(new QueryWrapper<Counselors>().eq("counselorId", appointments.getCounselorid())).getUserid();
        QueryWrapper<Useranalytics> wrapper = new QueryWrapper<Useranalytics>().eq("userId", userid);
        if (useranalyticsMapper.exists(wrapper)){
            //如果存在就修改
            Useranalytics one = useranalyticsMapper.selectOne(wrapper);
            one.setTotalappointments(one.getTotalappointments()+1);
            useranalyticsMapper.updateById(one);
        }else {
            Useranalytics useranalytics = new Useranalytics();
            useranalytics.setUserid(userid);
            useranalytics.setCounselorid(appointments.getCounselorid());
            useranalytics.setTotalappointments(1);
            useranalytics.setCanceledappointments(0);
            useranalytics.setCompletedappointments(0);
            useranalytics.setLastconsultation(new Date());
            useranalyticsMapper.insert(useranalytics);
        }
    }


    @Override
    public void addCancel(AppointmentRecordVo appointmentRecordVo) {
        QueryWrapper<Useranalytics> wrapper = new QueryWrapper<Useranalytics>().eq("userId", appointmentRecordVo.getUserCouID());
        Useranalytics one = useranalyticsMapper.selectOne(wrapper);
        one.setCanceledappointments(one.getCanceledappointments()+1);
        useranalyticsMapper.updateById(one);
    }

    @Override
    public void addComplete(AppointmentRecordVo appointmentRecordVo) {
        QueryWrapper<Useranalytics> wrapper = new QueryWrapper<Useranalytics>().eq("userId", appointmentRecordVo.getUserCouID());
        Useranalytics one = useranalyticsMapper.selectOne(wrapper);
        one.setCompletedappointments(one.getCompletedappointments()+1);
        useranalyticsMapper.updateById(one);
    }
}




